package com.hk.webapp.controller.client;

import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.bean.CmsCategory;
import com.hk.webapp.service.CmsCategoryService;
import com.hk.webapp.vo.ResultVo;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class CmsCategoryController extends ClientBaseController {

    @Resource
    private CmsCategoryService cmsCategoryService;

    @PostMapping("/Category/GetCategoryByPid")
    @ApiOperation("根据父id查询所有子级分类")
    @AllowAnonymous
    public ResultVo getCategoryByPid(Integer id){

        List<CmsCategory> list= cmsCategoryService.getEntityListByPid(id);

        return ResultVo.createBySuccessData(list);
    }

    @PostMapping("/Category/GetCategoryByCode")
    @ApiOperation("根据code值查询所有子级分类")
    @AllowAnonymous
    public ResultVo getCategoryByCode(String code){
        if (StringUtils.isBlank(code))
        {
            return ResultVo.createByError("参数不完整");
        }
        CmsCategory mod = cmsCategoryService.getEntityByCode(code);
        if (mod==null)
        {
            return ResultVo.createByError("分类不存在");
        }
        List<CmsCategory> list = cmsCategoryService.getEntityListByPid(mod.getId());

        return ResultVo.createBySuccessData(list.parallelStream().map(c->{
            HashMap<String,Object> map= Maps.newHashMap();
            map.put("id",c.getId());
            map.put("name",c.getName());
            map.put("class_name",c.getClassName());
            map.put("code",c.getCode());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("bak",c.getBak());
            map.put("bak2",c.getBak2());
            return map;
        }).collect(Collectors.toList()));
    }

    @PostMapping("/Category/GetModel")
    @ApiOperation("获取单个分类详情")
    public ResultVo GetModel(Integer id){
        if (id<=0){
            return ResultVo.createByError("非法参数");
        }
        CmsCategory mod= cmsCategoryService.getEntityById(id);
        if (mod==null)
        {
            return ResultVo.createByError("id不存在");
        }
        HashMap<String,Object> map= Maps.newHashMap();
        map.put("id",mod.getId());
        map.put("name",mod.getName());
        map.put("class_name",mod.getClassName());
        map.put("code",mod.getCode());
        map.put("img_url",mod.getImgUrl());
        map.put("imgs_url",mod.getImgsUrl());
        map.put("bak",mod.getBak());
        map.put("bak2",mod.getBak2());

        return ResultVo.createBySuccessData(map);
    }

    @ApiOperation("根据CODE获取单个分类详情")
    @PostMapping("/Category/GetModelByCode")
    @AllowAnonymous
    public ResultVo GetModelByCode(String code){
        CmsCategory mod = cmsCategoryService.getEntityByCode(code);
        if (mod == null)
        {
            return ResultVo.createByError("code不存在");
        }
        HashMap<String,Object> map= Maps.newHashMap();
        map.put("id",mod.getId());
        map.put("name",mod.getName());
        map.put("class_name",mod.getClassName());
        map.put("code",mod.getCode());
        map.put("img_url",mod.getImgUrl());
        map.put("imgs_url",mod.getImgsUrl());
        map.put("bak",mod.getBak());
        map.put("bak2",mod.getBak2());
        return ResultVo.createBySuccessData(map);
    }

}
